package com.lazy.mongodemo;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import org.bson.Document;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoCredential;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

public class DemoInsert {
    private static final Logger logger = LoggerFactory.getLogger(DemoInsert.class);

    private MongoClient client;
    private MongoCollection<Document> coll;

    public DemoInsert(String host, int port){
        List<MongoCredential> credentials = new ArrayList<MongoCredential>();
        // credentials.add(MongoCredential.createCredential("sms_test", "ecodeSms", "sms_test".toCharArray()));
        //client = new MongoClient(new ServerAddress("127.0.0.1", 27017), credentials);
        client = new MongoClient(host, port);
 
        MongoDatabase db = client.getDatabase("gwgps");
        coll = db.getCollection("location");
    }

//    static String json = "{ \"_id\": ObjectId(\"5656dabade19b41250b1a55a\"),    \"SEQID\": 213, \"HOSTID\": 30003311,   \"HOSTNO\": \"沪AMC601\",    \"POS_TIME\": \"2015-04-09 17:24:00.0\",    \"POS_DATAFMT\": 0, \"POS_LAT\": 30.924379, \"POS_LONG\": 121.310533,   \"POS_SPEED\": 100, \"POS_ANGLE\": 100, \"POS_STARS\": -1,  \"POS_SIGLEVEL\": \"00\",   \"GATENO\": 1,  \"REC_TIME\": \"2015-04-09 16:28:34.0\",    \"SAVEDTOHISREC\": 0,   \"POSDESCRIPTION\": \"*\",  \"ACCSTATUS\": -1,  \"GPSCHANNALTYPE\": 1,  \"HIGHBEAMSTATUS\": -1, \"LOWBEAMSTATUS\": -1,  \"LEFTTURNSTATUS\": -1, \"RIGHTTURNSTATUS\": -1,    \"BRAKESTATUS\": -1}";
    static String json = "{   \"SEQID\": 213, \"HOSTID\": 30003311, "
            + "  \"HOSTNO\": \"沪AMC601\",    \"POS_TIME\": \"2015-04-09 17:24:00.0\", "
            + "   \"POS_DATAFMT\": 0, \"POS_LAT\": 30.924379, \"POS_LONG\": 121.310533, "
            + "  \"POS_SPEED\": 100, \"POS_ANGLE\": 100, \"POS_STARS\": -1,  \"POS_SIGLEVEL\": \"00\",  "
            + " \"GATENO\": 1,  \"REC_TIME\": \"2015-04-09 16:28:34.0\",    \"SAVEDTOHISREC\": 0, "
            + "  \"POSDESCRIPTION\": \"*\",  \"ACCSTATUS\": -1,  \"GPSCHANNALTYPE\": 1,"
            + "  \"HIGHBEAMSTATUS\": -1, \"LOWBEAMSTATUS\": -1,  \"LEFTTURNSTATUS\": -1, \"RIGHTTURNSTATUS\": -1, "
            + "   \"BRAKESTATUS\": -1}";

    public  void testInsert() {
        coll.drop();
        List<Document> list = new ArrayList<Document>();
        long t1 = System.currentTimeMillis();
        for (int k = 0; k < 1000; k++) {
            insertBatch(1000);
        }
        long t2 = System.currentTimeMillis();
        
        logger.info("insert 1M docs takse {} ms", (t2-t1));
    }
    
    private void insertBatch(int batchLimit) {
        int c = 1;
        List<Document> list = new ArrayList<Document>();
        for (int i = 0; i < batchLimit; i++) {
            Document d = Document.parse(json);
            //d.put("_id", ObjectId.get());
            d.put("idx", c);
            list.add(d);
        }
        coll.insertMany(list);
    }


    public static void main(String[] args){
//        String host = "127.0.0.1";
        String host = "192.168.1.234";
        int port = 27017;
        int count = 1;
        if (args.length == 3) {
            host = args[0];
            port = Integer.valueOf(args[1]);
            count = Integer.valueOf(args[2]);
        }
        logger.info("connecting to mongodb {}:{}  group count {} ", host, port, count);
        logger.info("json = {}", json);
        DemoInsert demo = new DemoInsert(host, port);
        for (int i = 0; i < count; i++) {
            demo.testInsert();
        }
    }
 

}
